package com.itttq.kb.utils;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

/**
 * 系统静态变量类
 *
 * @createTime: 2014-11-25 上午9:44:34
 * @author: <a href="mailto:gaoxvguo060@haopinfeng.com">高绪国</a>
 * @version: 0.1
 * @changesSum: 
 * 
 */
public class Constants {
	/** 状态：开启，值为1 */
	public static final int STATUTS_OPEN = 1;
	/** 状态：关闭，值为2 */
	public static final int STATUTS_CLOSE = 2;
	/** 逗号分隔符：, */
	public static final String SEPARATOR_COMMA = ",";
	/** 反斜杠分隔符：/ */
	public static final String SEPARATOR_BACKSLASH = "/";
	/** 点分隔符：. */
	public static final String SEPARATOR_SPOT = ".";
	
	/** WEB项目使用 */
	/* 注册验证码 */
	public static final String SESSION_CODE_REG = "reg_code";
	/* 找回密码验证码 */
	public static final String SESSION_CODE_GETPWD = "getpwd_code";

	/** SESSION中保留的第三方唯一标识對象KEY值，值为appid */
	public static final String SESSION_APPID = "appid";
	/** SESSION中保留的手机号KEY值，值为mobile */
	public static final String SESSION_MOBIEL = "mobile";
	/** SESSION中保留的系统用戶對象KEY值，值为user */
	public static final String SESSION_USER = "user";
	/** SESSION中保留的系统上一次的URL，值为last_url */
	public static final String SESSION_URL_LAST = "last_url";
	public static List<String> IMG_TYPES = new ArrayList<String>();
	/** 上传文件目录：1、广告；2、产品；3、帮助；4、分类推荐；9、其他 */
	public static Map<Integer, String> UPLOAD_DIR = new HashMap<Integer, String>();
	static {
		IMG_TYPES.add(".jpg");
		IMG_TYPES.add(".png");
		IMG_TYPES.add(".gif");
		IMG_TYPES.add(".jpeg");
		UPLOAD_DIR.put(1, "avatar");
		UPLOAD_DIR.put(2, "topic");
		UPLOAD_DIR.put(3, "reply");
		UPLOAD_DIR.put(9, "other");
	}
	/**
	 * 按时间的毫秒级获取文件名称
	 * @return
	 */
	public static String getFilenameByTime() {
		Calendar cal = Calendar.getInstance();
		return String.valueOf(cal.get(Calendar.YEAR)) + cal.get(Calendar.MONTH) + cal.get(Calendar.DATE)  
				+ Constants.SEPARATOR_BACKSLASH + String.valueOf(cal.get(Calendar.MINUTE)) + cal.get(Calendar.SECOND) 
				+ cal.get(Calendar.MILLISECOND) + Constants.genRandomNum(6);
	}
	
	/**
	 * 获取指定位数的数字
	 * 
	 * @param len 指定长度
	 * @return
	 */
	public static String genRandomNum(int len) {
		//35是因为数组是从0开始的，26个字母+10个数字
		final int maxNum = 36;
		int i; //生成的随机数
		int count = 0; //生成的密码的长度
		char[] str = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
		StringBuffer pwd = new StringBuffer("");
		Random r = new Random();
		while (count < len) {
			//生成随机数，取绝对值，防止生成负数，
			i = Math.abs(r.nextInt(maxNum)); //生成的数最大为36-1
			if (i >= 0 && i < str.length) {
				pwd.append(str[i]);
				count++;
			}
		}
		return pwd.toString();
	}
	/**
	 * 生成指定范围内的随机数
	 * 
	 * @param min	最大值（生成里面会包含此数字）
	 * @param max	最小值（生成里面会包含此数字）
	 * @return
	 * @Author 迷蝶
	 * @return int
	 */
	public static int genRandomNum(int min, int max) {
	    Random random = new Random();
	    return random.nextInt(max)%(max-min+1) + min;
	}
	/**
	 * 判断字符串是不是数字
	 * @param str
	 * @return
	 */
	public static boolean isNumeric(String str) {
		for (int i = 0; i < str.length(); i++) {
			if (!Character.isDigit(str.charAt(i))) {
				return false;
			}
		}
		return true;
	}
	
	public static String getIp(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
			if (ip.equals("127.0.0.1") || ip.equals("0:0:0:0:0:0:0:1")) {
				// 根据网卡取本机配置的IP
				InetAddress inet = null;
				try {
					inet = InetAddress.getLocalHost();
				} catch (UnknownHostException e) {
					e.printStackTrace();
				}
				ip = inet.getHostAddress();
			}
		}
		// 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
		if (ip != null && ip.length() > 15) {
			if (ip.indexOf(",") > 0) {
				ip = ip.substring(0, ip.indexOf(","));
			}
		}
		return ip;
	}
}
